import 'package:flutter/material.dart';

import 'app_theme.dart';

class HomeDrawer extends StatefulWidget {
  const HomeDrawer(
      {required this.screenIndex, required this.callBackIndex, Key? key})
      : super(key: key);

  final DrawerIndex screenIndex;
  final Function(DrawerList) callBackIndex;

  @override
  _HomeDrawerState createState() => _HomeDrawerState();
}

class _HomeDrawerState extends State<HomeDrawer> {
  late final List<DrawerList> drawerList;

  @override
  void initState() {
    super.initState();
    setDrawerListArray();
  }

  void setDrawerListArray() {
    drawerList = <DrawerList>[
      DrawerList(
        index: DrawerIndex.home,
        labelName: '地图',
        icon: const Icon(Icons.home),
      ),
      DrawerList(
        index: DrawerIndex.core,
        labelName: '核心干线',
        icon: const Icon(Icons.map_outlined),
      ),
      DrawerList(
        index: DrawerIndex.easy,
        labelName: '短线休闲',
        arrayIndex:1,
        icon: const Icon(Icons.list),
      ),
      DrawerList(
        index: DrawerIndex.high,
        labelName: '赛事精选',
        arrayIndex:2,
        icon: const Icon(Icons.group),
      ),
      DrawerList(
        index: DrawerIndex.alone,
        labelName: '野路精选',
        arrayIndex:3,
        icon: const Icon(Icons.highlight),
      ),
      DrawerList(
        index: DrawerIndex.supers,
        labelName: '实验室',
        icon: const Icon(Icons.room_preferences),
      ),
      DrawerList(
        index: DrawerIndex.about,
        labelName: '关于',
        icon: const Icon(Icons.info),
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(
          0, MediaQuery.of(context).padding.top + 10, 12, 12),
      width: MediaQuery.of(context).size.width * 0.35,
      decoration: const BoxDecoration(
        color: Colors.black12,
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          const Padding(padding: EdgeInsets.only(left: 12),child:
          CircleAvatar(
            radius: 36,
            backgroundImage: NetworkImage(
                'https://img0.baidu.com/it/u=1931543473,3902628289&fm=26&fmt=auto'),
          ),),
          const SizedBox(
            height: 4,
          ),
          Expanded(
            child: ListView.builder(
              physics: const BouncingScrollPhysics(),
              padding: const EdgeInsets.all(0.0),
              itemCount: drawerList.length,
              itemBuilder: (BuildContext context, int index) {
                return inkwell(drawerList[index]);
              },
            ),
          ),
          Divider(
            height: 1,
            color: AppTheme.grey.withOpacity(0.6),
          ),
          Column(
            children: <Widget>[
              Material(
                color: Colors.transparent,
                child: InkWell(
                  child: ListTile(
                    title: const Text(
                      'Sign Out',
                      style: TextStyle(
                        fontFamily: AppTheme.fontName,
                        fontWeight: FontWeight.w600,
                        fontSize: 16,
                        color: AppTheme.darkText,
                      ),
                      textAlign: TextAlign.left,
                    ),
                    trailing: const Icon(
                      Icons.power_settings_new,
                      color: Colors.red,
                    ),
                    onTap: () {},
                  ),
                ),
              ),
              SizedBox(
                height: MediaQuery.of(context).padding.bottom,
              )
            ],
          ),
        ],
      ),
    );
  }

  Widget inkwell(DrawerList listData) {
    return Material(
      color: Colors.transparent,
      child: InkWell(
        splashColor: Colors.grey.withOpacity(0.1),
        highlightColor: Colors.transparent,
        onTap: () {
          navigator(listData);
        },
        child: Stack(
          children: <Widget>[
            Container(
              padding: const EdgeInsets.only(top: 8.0, bottom: 8.0),
              child: Row(
                children: <Widget>[
                  const SizedBox(
                    width: 6.0,
                    height: 36.0,
                  ),
                  const Padding(
                    padding: EdgeInsets.all(4.0),
                  ),
                  if (listData.isAssetsImage)
                    SizedBox(
                      width: 24,
                      height: 24,
                      child: Image.asset(listData.imageName,
                          color: widget.screenIndex == listData.index
                              ? Colors.blue
                              : AppTheme.nearlyBlack),
                    )
                  else
                    Icon(listData.icon?.icon,
                        color: widget.screenIndex == listData.index
                            ? Colors.white
                            : AppTheme.nearlyWhite),
                  const Padding(
                    padding: EdgeInsets.all(4.0),
                  ),
                  Text(
                    listData.labelName,
                    style: TextStyle(
                      fontWeight: FontWeight.w500,
                      fontSize: 16,
                      color: widget.screenIndex == listData.index
                          ? Colors.white
                          : AppTheme.nearlyWhite,
                    ),
                    textAlign: TextAlign.left,
                  ),
                ],
              ),
            ),
           const SizedBox()
          ],
        ),
      ),
    );
  }

  Future<void> navigator(DrawerList indexScreen) async {
    widget.callBackIndex(indexScreen);

  }
}

enum DrawerIndex { home, core, high, easy, alone, about, supers }

class DrawerList {
  DrawerList({
    required this.index,
    this.icon,
    this.isAssetsImage = false,
    this.labelName = '',
    this.imageName = '',
    this.arrayIndex = 0,
  });

  String labelName;
  Icon? icon;
  bool isAssetsImage;
  String imageName;
  DrawerIndex index;
  int arrayIndex;
}
